package com.burstly.lib.network.request;

import com.burstly.lib.network.TimeoutManager;
import com.burstly.lib.network.beans.cookie.ICookieRequest;
import com.burstly.lib.util.LoggerExt;
import com.burstly.lib.util.Utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import junit.framework.Assert;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

class BurstlyRequestTask<T> extends AbortableAsyncTask<T>
{
  static final LoggerExt LOG = LoggerExt.getInstance();
  private static final int MAX_RESPONSE_LENGTH = 100000;
  private IRequestCallback<T> mCallback;
  private Object mObjToPost;
  private IResponseConverter<T> mResponseConverter;
  private String mSerializedObjectToPost;
  private final Class<T> mTartgetClass;

  BurstlyRequestTask(ICookieRequest paramICookieRequest, String paramString1, Class<T> paramClass, String paramString2)
  {
    super(paramString1, paramString2);
    Assert.assertNotNull("Target class to convert can not be NULL." + getClass().getCanonicalName(), paramClass);
    this.mTartgetClass = paramClass;
    this.mResponseConverter = ConverterFactory.getConverter(paramClass);
    if (paramICookieRequest != null)
    {
      this.mObjToPost = paramICookieRequest;
      paramICookieRequest.setValidCookies();
    }
  }

  protected T convertServerResponse(String paramString)
  {
    if (this.mResponseConverter != null)
      return this.mResponseConverter.convertResponse(paramString);
    Object[] arrayOfObject = new Object[1];
    arrayOfObject[0] = this.mTartgetClass.getCanonicalName();
    logMessage(6, "No valid converter could be found for {0}", arrayOfObject);
    return null;
  }

  public IRequestCallback<T> getCallback()
  {
    return this.mCallback;
  }

  protected String getDataToPost()
  {
    if (this.mObjToPost != null)
    {
      if (this.mSerializedObjectToPost == null)
        this.mSerializedObjectToPost = Utils.toJson(this.mObjToPost);
      return this.mSerializedObjectToPost;
    }
    return "";
  }

  public Class<T> getTartgetClass()
  {
    return this.mTartgetClass;
  }

  protected String handleConnectLogic(HttpHost paramHttpHost, HttpPost paramHttpPost)
    throws UnsupportedEncodingException, IOException, ClientProtocolException
  {
    String str1 = getDataToPost();
    logMessage(3, "Request data to post to server: {0}", new Object[] { str1 });
    paramHttpPost.setEntity(new StringEntity(str1, "UTF-8"));
    HttpResponse localHttpResponse = Utils.makeRequest(paramHttpHost, paramHttpPost);
    int i = localHttpResponse.getStatusLine().getStatusCode();
    if (i != 200)
    {
      Object[] arrayOfObject3 = new Object[2];
      arrayOfObject3[0] = paramHttpHost.getHostName();
      arrayOfObject3[1] = Integer.valueOf(i);
      logMessage(6, "Request to {0} has finished with {1} http status.", arrayOfObject3);
      return null;
    }
    HttpEntity localHttpEntity = localHttpResponse.getEntity();
    long l = localHttpEntity.getContentLength();
    if (l >= 100000L)
    {
      Object[] arrayOfObject2 = new Object[1];
      arrayOfObject2[0] = Long.valueOf(l);
      logMessage(6, "Response length is too big to handle: {0}", arrayOfObject2);
      return null;
    }
    Object[] arrayOfObject1 = new Object[1];
    arrayOfObject1[0] = Long.valueOf(l);
    logMessage(3, "Raw response data length: {0}", arrayOfObject1);
    String str2 = EntityUtils.toString(localHttpEntity, "UTF-8");
    logMessage(3, "Raw response data: {0}", new Object[] { str2 });
    return str2;
  }

  void logMessage(int paramInt, String paramString, Object[] paramArrayOfObject)
  {
    String str = "(BVID: " + getBurstlyViewId() + ") " + paramString;
    switch (paramInt)
    {
    default:
      LOG.logDebug("BurstlyRequestTask", str, paramArrayOfObject);
      return;
    case 3:
      LOG.logDebug("BurstlyRequestTask", str, paramArrayOfObject);
      return;
    case 6:
      LOG.logError("BurstlyRequestTask", str, paramArrayOfObject);
      return;
    case 4:
      LOG.logInfo("BurstlyRequestTask", str, paramArrayOfObject);
      return;
    case 5:
    }
    LOG.logWarning("BurstlyRequestTask", str, paramArrayOfObject);
  }

  protected void onPostExecute(T paramT)
  {
    if ((this.mCallback != null) && (!isAborted()))
    {
      if (paramT == null)
        break label39;
      this.mCallback.onSuccessUi(paramT);
    }
    while (true)
    {
      this.mCallback = null;
      super.onPostExecute(paramT);
      return;
      label39: this.mCallback.onFailUi(null);
    }
  }

  protected void onPreExecute()
  {
    super.onPreExecute();
    if (this.mCallback != null)
      this.mCallback.preExecute();
  }

  protected T performInBackground()
  {
    Object localObject = super.performInBackground();
    if ((this.mCallback != null) && (!isAborted()))
    {
      if (localObject != null)
        this.mCallback.onSuccessInBackground(localObject);
    }
    else
      return localObject;
    this.mCallback.onFailInBackground(null);
    return localObject;
  }

  public void setCallback(IRequestCallback<T> paramIRequestCallback)
  {
    this.mCallback = paramIRequestCallback;
  }

  protected String tryToConnect(HttpHost paramHttpHost, HttpPost paramHttpPost)
  {
    try
    {
      String str = handleConnectLogic(paramHttpHost, paramHttpPost);
      return str;
    }
    catch (ConnectTimeoutException localConnectTimeoutException)
    {
      Object[] arrayOfObject3 = new Object[2];
      arrayOfObject3[0] = Integer.valueOf(TimeoutManager.getConnectTimeout());
      arrayOfObject3[1] = paramHttpHost.getHostName();
      logMessage(5, "Could not establish connection for {0} to {1} host", arrayOfObject3);
      return null;
    }
    catch (SocketTimeoutException localSocketTimeoutException)
    {
      while (true)
      {
        Object[] arrayOfObject2 = new Object[2];
        arrayOfObject2[0] = Integer.valueOf(TimeoutManager.getReadimeout());
        arrayOfObject2[1] = paramHttpHost.getHostName();
        logMessage(5, "Could not get response for {0} millisec from {1}", arrayOfObject2);
      }
    }
    catch (Exception localException)
    {
      while (true)
      {
        Object[] arrayOfObject1 = new Object[1];
        arrayOfObject1[0] = paramHttpHost.getHostName();
        logMessage(5, "Request to {0} failed.", arrayOfObject1);
      }
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.network.request.BurstlyRequestTask
 * JD-Core Version:    0.6.2
 */